<?php

/**
 * Abstract value class
 *
 * @author    Mon Zafra <monzee at gmail>
 * @copyright (c)2009 Mon Zafra
 * @license   http://monzee.wordpress.com/license New BSD
 * @version   $Id: Value.php 38 2009-02-18 17:12:39Z monzee $
 */
abstract class Admin_Model_Value implements IteratorAggregate, ArrayAccess
{
    protected $_data;

    public function __construct($data = null)
    {
        if (isset($data['data'])) {
            $this->setData($data['data']);
        }
    }

    public function setData($data)
    {
        $this->_data = $data;
    }

    public function __get($field)
    {
        $data = $this->_data;

        if (!isset($data[$field])) {
            throw new Exception("$field is not a valid field");
        }

        return $data[$field];
    }

    public function __set($field, $value)
    {
        $this->_data[$field] = $value;
    }

    public function toArray()
    {
        return $this->_data;
    }

    /**
     * Iterator aggregate implementation
     *
     * @return ArrayIterator
     */
    public function getIterator()
    {
        return new ArrayIterator($this->_data);
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     */
    public function offsetExists($offset)
    {
        return isset($this->_data[$offset]);
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     */
    public function offsetGet($offset)
    {
        return $this->_data[$offset];
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     * @param mixed $value
     */
    public function offsetSet($offset, $value)
    {
        $this->_data[$offset] = $value;
    }

    /**
     * Array access implementation
     *
     * @param mixed $offset
     */
    public function offsetUnset($offset)
    {
        unset($this->_data[$offset]);
    }
}
